package controller;

import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import model.entity.ReinsuranceEnsuredEntity;
import baseclass.BaseCreateBill;
import baseclass.ErrorPage;

public class CreateEnsured extends BaseCreateBill<ReinsuranceEnsuredEntity>
{
    public CreateEnsured() throws IOException, WriteException
    {
	super();
    }

    /**
     * 
     */
    private static final long serialVersionUID=1L;

    public void create() throws BiffException, IOException, WriteException
    {
	writeWorkBook=Workbook.createWorkbook(new File("D:\\批改帳單"+date+".xls"));
	setSheet();
	getTotalbyte();
	type=0;
	row=1;
	title(writeSheet[0],0,"0");
	for(ReinsuranceEnsuredEntity bill : bills)
	{
	    ReinsuranceEnsured=bill;
	    setProgressBar();
	    setBody();
	    if("SUB-TOTAL:".equals(bill.getWORK_ENDOR_CESSION_NO()))
	    {
		type=Integer.parseInt(bill.getType());
		title(writeSheet[type],0,bill.getType());
		row=1;
	    }
	}
	writeWorkBook.write();
	writeWorkBook.close();
	JOptionPane.showMessageDialog(null,"檔案完成");
	dispose();
    }

    public void title(WritableSheet sheet, int row, String type)
	    throws RowsExceededException, WriteException
    {
	sheet.addCell((new Label(0,row,"Endor Cession NO",cellFormat1)));
	sheet.addCell((new Label(1,row,"Cession NO",cellFormat1)));
	sheet.addCell((new Label(2,row,"保單號",cellFormat1)));
	sheet.addCell((new Label(3,row,"批單號",cellFormat1)));
	sheet.addCell((new Label(4,row,"加保額",cellFormat1)));
	sheet.addCell((new Label(5,row,"減保額",cellFormat1)));
	sheet.addCell((new Label(6,row,"加保費",cellFormat1)));
	sheet.addCell((new Label(7,row,"減保費",cellFormat1)));
	sheet.addCell((new Label(8,row,"新費率",cellFormat1)));
	sheet.addCell((new Label(9,row,"舊費率",cellFormat1)));
	sheet.addCell((new Label(10,row,"分子",cellFormat1)));
	sheet.addCell((new Label(11,row,"分母",cellFormat1)));
    }

    public void setBody() throws RowsExceededException, WriteException
    {
	writeSheet[type].addCell(new Label(0,row,ReinsuranceEnsured.getWORK_ENDOR_CESSION_NO(),cellFormat1));
	writeSheet[type].addCell(new Label(1,row,ReinsuranceEnsured.getWORK_CESSION(),cellFormat1));
	writeSheet[type].addCell(new Label(2,row,ReinsuranceEnsured.getWORK_PLY(),cellFormat1));
	writeSheet[type].addCell(new Label(3,row,ReinsuranceEnsured.getWORK_ENDOR_NO(),cellFormat1));
	writeSheet[type].addCell(new Label(4,row,ReinsuranceEnsured.getWORK_ADD_AMT(),cellFormat1));
	writeSheet[type].addCell(new Label(5,row,ReinsuranceEnsured.getWORK_SUBSTRACT_AMT(),cellFormat1));
	writeSheet[type].addCell(new Label(6,row,ReinsuranceEnsured.getWORK_ADD_PREM(),cellFormat1));
	writeSheet[type].addCell(new Label(7,row,ReinsuranceEnsured.getWORK_SUBSTRACT_PREM(),cellFormat1));
	writeSheet[type].addCell(new Label(8,row,ReinsuranceEnsured.getWORK_AVG_RATE_N(),cellFormat1));
	writeSheet[type].addCell(new Label(9,row,ReinsuranceEnsured.getWORK_AVG_RATE_O(),cellFormat1));
	writeSheet[type].addCell(new Label(10,row,ReinsuranceEnsured.getWORK_DAYS(),cellFormat1));
	writeSheet[type].addCell(new Label(11,row,ReinsuranceEnsured.getWORK_DAYSA(),cellFormat1));
	row=row+1;
    };

    @Override
    public void run()
    {
	try
	{
	    create();
	}
	catch (Exception e)
	{
	    new ErrorPage(e);
	}
    }

}
